<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select v-model="listQuery.type" placeholder="活动类型" clearable style="width: 120px">
        <el-option label="考试" :value="1"/>
        <el-option label="测评" :value="2"/>
        <el-option label="作业" :value="3"/>
      </el-select>

      <el-input
        v-model="listQuery.searchKey"
        placeholder="搜索考试名称"
        style="width: 200px; margin-left: 10px"
        clearable
        @keyup.enter.native="handleFilter"
      />

      <el-button
        type="primary"
        style="margin-left: 10px"
        @click="handleFilter"
      >
        搜索
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%; margin-top: 20px"
    >
      <el-table-column label="考试/测评" prop="title" min-width="200"/>
      <el-table-column label="类型" width="100">
        <template slot-scope="{row}">
          <el-tag :type="row.type | typeFilter">
            {{ row.type | typeNameFilter }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" prop="endTime" width="180">
        <template slot-scope="{row}">
          {{ row.endTime }}
        </template>
      </el-table-column>
      <el-table-column label="参与人数" prop="participantCount" width="100"/>
      <el-table-column label="平均分" prop="avgScore" width="100"/>
      <el-table-column label="通过率" prop="passRate" width="100">
        <template slot-scope="{row}">
          {{ row.passRate }}%
        </template>
      </el-table-column>
      <el-table-column label="最高分" prop="highestScore" width="100"/>
      <el-table-column label="最低分" prop="lowestScore" width="100"/>
      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="{row}">
          <el-button
            type="primary"
            size="mini"
            @click="handleDetail(row.id)"
          >
            详情
          </el-button>
          <el-button
            v-if="row.scorePubedStatus === 0"
            type="success"
            size="mini"
            @click="handlePublish(row.id)"
          >
            发布
          </el-button>
          <el-button
            v-else="row.scorePubedStatus === 1"
            type="success"
            size="mini"
            @click="handlePublish(row.id)"
          >
            撤回
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {fetchScoreList, publishScore} from '@/api/exam/score'
import Pagination from '@/components/Pagination'
import {parseTime} from '@/utils/quiz'

export default {
  name: 'ScoreList',
  components: {Pagination},
  props: {
    groupId: {
      type: [String, Number],
      required: true
    }
  },
  filters: {
    typeFilter(type) {
      const typeMap = {
        1: 'danger',
        2: 'success',
        3: 'info'
      }
      return typeMap[type]
    },
    typeNameFilter(type) {
      const typeNameMap = {
        1: '考试',
        2: '测评',
        3: '作业'
      }
      return typeNameMap[type]
    }
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        groupId: undefined, // 将从props接收
        type: undefined,
        searchKey: undefined
      }
    }
  },
  watch: {
    groupId: {
      immediate: true,
      handler(newVal) {
        if (newVal) {
          this.listQuery.groupId = newVal
          this.getList()
        }
      }
    }
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchScoreList(this.listQuery).then(response => {
        this.list = response.rows
        this.total = response.total
        this.listLoading = false
      }).catch(() => {
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleDetail(id) {
      this.$router.push({
        name: 'ScoreDetail',
        path: '/score/detail',
        query: {
          examId: id,
          groupId: this.listQuery.groupId
        }
      })
    },
    handlePublish(id) {
      this.$confirm('确定要发布成绩吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        publishScore(id).then(() => {
          this.$message({
            type: 'success',
            message: '发布成功!'
          })
          this.getList()
        })
      })
    },
    // 时间格式化
    parseTime(time) {
      return parseTime(time, '{y}-{m}-{d} {h}:{i}:{s}')
    }
  }
}
</script>

<style scoped>
.filter-container {
  margin-bottom: 20px;
}
</style>
